package ScoreBoard.Login.Model;

import ScoreBoard.Login.View.*;
import ScoreBoard.Login.Model.*;
import ScoreBoard.Login.Controller.*;

import Exception.LoginFailureException;
import Exception.SocketConnectionFailureException;

import java.io.*;
import java.net.*;

import Core.Model.*;

public class ScoreBoardLoginModel 
{
	public void initialize() 
	{
		
	}

	public void setViewAndController(ScoreBoardLoginView view,
			ScoreBoardLoginController controller) 
	{
		this.view = view;
		this.controller = controller;
	}

	public boolean login(String userName, char[] password)
			throws SocketConnectionFailureException, LoginFailureException 
    {
		try 
		{
			InitializeSocket setSocket = new InitializeSocket(this);
			if (setSocket.initializeSocket() == true) 
			{
				ServerConnection serverConnection = new ServerConnection(this.socket, ClientType.SCOREBOARD);
				try
				{
					if (serverConnection.login(userName, new String(password))) 
					{
						return true;
					}
				}
				catch (LoginFailureException exception) 
				{
					throw exception;
				}
			}
		} 
		catch (SocketConnectionFailureException exception) 
		{
			throw exception;
		}
		return true;
	}
    
	public Socket getSocket()
	{
		return this.socket;
	}
	
	private ScoreBoardLoginView view;
	private ScoreBoardLoginController controller;
	
	public Socket socket;
}
